package com.molichuxing.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.widget.Toast;


/**
 * 全局提示工具
 *
 * @Warning 只能在 UI 线程调用
 */
public final class Tip {

    private static Toast tip;
    private static Toast tiplong;

    private static void init(Context context) {
        tip = Toast.makeText(context.getApplicationContext(), "-", Toast.LENGTH_SHORT);
    }

    private static void initlong(Context context) {
        tiplong = Toast.makeText(context.getApplicationContext(), "-", Toast.LENGTH_LONG);
    }

    public static void showTip(Context context, String message) {
        if (message == null) {
            message = "--";
            return;
        }

        if (tip == null) {
            init(context);
        }

        if (tip != null) {
            tip.setText(message);
            Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
            // 获取屏幕高度
            int height = display.getHeight();
            // 这里给了一个1/4屏幕高度的y轴偏移量
            tip.setGravity(Gravity.TOP, 0, height / 4);
            tip.show();
        } else {
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

    public static void showTipLong(Context context, String message) {
        if (message == null) {
            message = "--";
            return;
        }

        if (tiplong == null) {
            initlong(context);
        }

        if (tiplong != null) {
            tiplong.setText(message);
            Display display = ((Activity)context).getWindowManager().getDefaultDisplay();
            // 获取屏幕高度
            int height = display.getHeight();
            // 这里给了一个1/4屏幕高度的y轴偏移量
            tiplong.setGravity(Gravity.TOP, 0, height / 4);
            tiplong.show();
        } else {
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
    }

}
